package com.thoughtworks.collection;

import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Add {
    public int getSumOfEvens(int leftBorder, int rightBorder) {
        int max = 0;
        int min = 0;
        int num = 0;
        if (leftBorder < rightBorder) {
            min = leftBorder;
            max = rightBorder;
        } else {
            min = rightBorder;
            max = leftBorder;
        }
        for (int i = min; i <= max; i++) {
            if (i % 2 == 0) {
                num = i + num;
            }
        }
        return num;
    }

    public int getSumOfOdds(int leftBorder, int rightBorder) {
        int max = 0;
        int min = 0;
        int num = 0;
        if (leftBorder < rightBorder) {
            min = leftBorder;
            max = rightBorder;
        } else {
            min = rightBorder;
            max = leftBorder;
        }
        for (int i = min; i <= max; i++) {
            if (i % 2 != 0) {
                num = i + num;
            }
        }
        return num;
    }

    public int getSumTripleAndAddTwo(List<Integer> arrayList) {
        int num = 0;
        for (Integer i : arrayList) {
            num = (i * 3 + 2) + num;
        }
        return num;
    }

    public List<Integer> getTripleOfOddAndAddTwo(List<Integer> arrayList) {
        List list = new ArrayList();
        for (Integer i : arrayList) {
            if (i % 2 != 0) {
                i = i * 3 + 2;
            }
            list.add(i);
        }
        return list;
    }

    public int getSumOfProcessedOdds(List<Integer> arrayList) {
        int num = 0;
        for (Integer i : arrayList) {
            if (i % 2 != 0) {
                num = (i * 3 + 5) + num;
            }

        }
        return num;
    }

    public double getMedianOfEven(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        double num = 0;
        int a = 1;
        for (Integer i : arrayList) {
            if (i % 2 == 0) {
                list.add(i);
            }
        }
        if (list.size() % 2 == 0) {
            num = (list.get(list.size() / 2 - 1) + list.get(list.size() / 2)) / 2;
        } else {
            num = list.get(list.size() / 2 + 1);
        }

        return num;
    }

    public double getAverageOfEven(List<Integer> arrayList) {
        int num = 0;
        int t = 0;
        double p = 0;
        for (Integer i : arrayList) {
            if (i % 2 == 0) {
                num = i + num;
                t++;
            }

        }
        p = num / t;
        return p;
    }

    public boolean isIncludedInEvenIndex(List<Integer> arrayList, Integer specialElment) {
        List<Integer> list = new ArrayList<>();
        boolean b = false;
        for (Integer i : arrayList) {
            if (i % 2 == 0) {
                list.add(i);
            }
        }
        for (Integer it : list) {
            if (it.equals(specialElment)) {
                b = true;
            }
        }
        return b;
    }

    public List<Integer> getUnrepeatedFromEvenIndex(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        for (Integer i : arrayList) {
            if (i % 2 == 0) {
                list.add(i);
            }
        }
        for (int i = list.size() - 1; i > 0; i--) {
            for (int j = 0; j < i; j++) {
                if (list.get(j).equals(list.get(i))) {
                    list.remove(i);
                }
            }
        }
        return list;
    }

    public List<Integer> sortByEvenAndOdd(List<Integer> arrayList) {
        List<Integer> list1 = new ArrayList<>();
        List<Integer> list2 = new ArrayList<>();
        for (Integer i : arrayList) {
            if (i % 2 == 0) {
                list1.add(i);
            } else {
                list2.add(i);

            }
        }
        Collections.sort(list1);//从小到大
        Collections.sort(list2, Collections.reverseOrder());//从大到校小
        list2.stream().sequential().collect(Collectors.toCollection(() -> list1));
        return list1;
    }

    public List<Integer> getProcessedList(List<Integer> arrayList) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < arrayList.size() - 1; i++) {
            int r = (arrayList.get(i) + arrayList.get(i + 1)) * 3;
            list.add(r);
        }
        return list;
    }
}
